#include "projectopener.h"

ProjectOpener::ProjectOpener()
{
    projOpened = false;
}

ProjectOpener::~ProjectOpener()
{
    if(db.isOpen())
    {
        QSqlDatabase::database("open").close();
    }
}

void ProjectOpener::addFile(const QString &fileName)
{
    QFile file(fileName);

    if(!QSqlDatabase::contains(QString("open")))
        db = QSqlDatabase::addDatabase("QSQLITE", "open");
    else
    {
        db = QSqlDatabase::database(QString("open"));
        if(!db.isOpen()) db.open();
    }

    if(file.open(QIODevice::ReadOnly))
    {
        fileProcess(file);
        file.close();
    }
}

// Process sas file to be saved to database
// Sas output file of type .txt contains
// header lines which are project's main info.
void ProjectOpener::fileProcess(QFile &file)
{
    QTextStream in(&file);
    QString line;
    int mode;

    while(!in.atEnd())
    {
        line = in.readLine().simplified();

        if(line.contains("<<<Database>>>"))
        {
            mode = 0;
            continue;
        }
        else if(line.contains("<<<Experiment Header Information>>>"))
        {
            mode = 1;
            continue;
        }
        else if(line.contains("<<<Image Files>>>"))
        {
            mode = 2;
            continue;
        }
        else if(line.contains("<<<Number of pdn>>>"))
        {
            mode = 3;
            continue;
        }
        else if(line.contains(" <<<PDN's>>>"))
        {
            mode = 4;
            continue;
        }
        else if(line.contains("<<<Number of family>>>"))
        {
            mode = 5;
            continue;
        }
        else if(line.contains("<<<Family's>>>"))
        {
            mode = 6;
            continue;
        }
        else if(line.contains("<<<Image Directory>>>"))
        {
            mode = 7;
            continue;
        }
        else if(line.contains("<<<Experiment Imbibition Time>>>"))
        {
            mode = 8;
            continue;
        }

        //////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////
        if(mode == 0)
        {
            db.setDatabaseName(line);
            db.open();
        }
        if(mode == 1)
        {
            projectHeader = line;
            projOpened = true;
        }
        else if(mode == 2)
        {
            files.append(line);
        }
        else if (mode == 3)
        {
            pdn = line.toInt();
        }
        else if(mode == 4)
        {
            pdns.append(line);
        }
        else if(mode == 5)
        {
            family = line.toInt();
        }
        else if (mode == 6)
        {
            families.append(line);
        }
        else if( mode == 7)
        {
            dir = line;
        }
        else if( mode == 8)
        {
            start = line;
        }
    }
}

QStringList ProjectOpener::getPDN(QString &family)
{
    openDB();

    {
        QSqlQuery query = QSqlQuery(db);

        query.prepare("select distinct(pdn) from experiment where family like ?;");
        query.addBindValue("%"+family+"%");
        query.exec();

        while(query.next())
        {
            pdnByFamily.append(query.value(0).toString());
        }
    }
    return pdnByFamily;
}

QStringList ProjectOpener::getTime(QString &family, QString &pdn)
{
    openDB();

    {
        QSqlQuery query = QSqlQuery(db);

        query.prepare("select distinct(time) from experiment where family=? and pdn=?;");
        query.addBindValue(family);
        query.addBindValue(pdn);
        query.exec();

        while(query.next())
        {
            timeList.append(query.value(0).toString());
        }
    }
    return timeList;
}

void ProjectOpener::openDB()
{
    db.open();
}
